package cn.gl.org.controller;

import cn.gl.basic.util.ExcelUtils;
import cn.gl.org.domain.ShopAuditLog;
import cn.gl.org.dto.ShopDto;
import cn.gl.org.service.IShopService;
import cn.gl.org.domain.Shop;
import cn.gl.org.query.ShopQuery;
import cn.gl.basic.util.AjaxResult;
import cn.gl.basic.util.PageList;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public IShopService shopService;

    /**
     * 接口：添加或修改
     * @param shop  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.insert(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
                shopService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public Shop findOne(@PathVariable("id")Long id) {
        return shopService.laodOne(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Shop> findAll(){
        return shopService.laodAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }
    /**
     * 接口：店铺入驻
     * @param
     * @return AjaxResult 响应给前端
     */
    @PostMapping(value="/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }
    /**
     * 接口：店铺审核通过
     * @param
     * @return AjaxResult
     */
    @PostMapping(value="/audit/pass")
    @ApiOperation(value = "店铺审核通过")
    public AjaxResult pass(@RequestBody ShopAuditLog log){
        try {
            shopService.auditpass(log);
            return new AjaxResult();//true+"操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    /**
     * 接口：店铺审核驳回
     * @param
     * @return AjaxResult
     */
    @PostMapping(value="/audit/reject")
    @ApiOperation(value = "店铺审核驳回")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult reject(@RequestBody ShopAuditLog log){
        try {
            shopService.auditreject(log);
            return new AjaxResult();//true+"操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    @GetMapping(value = "/active/{shopId}")
    @ApiOperation(value = "店铺审核驳回")
    public void activation(@PathVariable("shopId") Long shopId){
        shopService.activation(shopId);
    }

    /**
     * 店铺信息导出
     * @param response
     */
    @GetMapping("/export")
    public void export (HttpServletResponse response){
        List<Shop> shops = shopService.laodAll();
        ExcelUtils.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);

    }
    /**
     * 店铺信息导入
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart MultipartFile file) {
        //MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass
        //titleRows - 标题的行数 headerRows头的行数
        List<Shop> shops = ExcelUtils.importExcel(file, 0, 1, Shop.class);
        shops.forEach(res -> {
            System.out.println(res);
        });
    }
    @GetMapping("echarts")
    public  List<ShopDto>echarts() {
        return  shopService.echarts();
    }

}
